class RankingUI extends egret.Sprite {

    public constructor() {
        super();
        //this.createView();
        this.once(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }
    private createView(): void {
        //添加奖牌图片+"全国排名"+名次
        var jiangpai = createBitmap("jiangpai_png", 216, 0);
        this.addChild(jiangpai);
        var text1 = createTextFiled("全国排名", 285, 205, 18, 0xff4c22);
        this.addChild(text1);
        var pm_text = createTextFiled("489", 240, 240, 55, 0xff4c22, "center", 170, 55);
        this.addChild(pm_text);
        //添加头像容器（头像+名字）
        var imgDisplay = new egret.DisplayObjectContainer();
        imgDisplay.width = 109;
        imgDisplay.height = 145;
        imgDisplay.x = 88;
        imgDisplay.y = 188;
        this.addChild(imgDisplay);
        var imgBox = new egret.Shape();
        imgBox.graphics.beginFill(0xffffff);
        imgBox.graphics.drawRoundRect(0, 0, 108, 108, 108, 108);
        imgBox.graphics.endFill;
        imgDisplay.addChild(imgBox);
        var name_text = createTextFiled("Amy", 0, 109, 28, 0xffffff, "center", 109, 36);
        imgDisplay.addChild(name_text);
        //添加time容器+"Time"+时间
        var timeDisplay = new egret.DisplayObjectContainer();
        timeDisplay.width = 148;
        timeDisplay.height = 58;
        timeDisplay.x = 429;
        timeDisplay.y = 224;
        this.addChild(timeDisplay);
        var time_title = createTextFiled("Time", 0, 0, 16, 0xffffff, "center", 148, 16);
        timeDisplay.addChild(time_title);
        var time_text = createTextFiled("25.00", 0, 23, 35, 0xffffff, "center", 148, 35);
        timeDisplay.addChild(time_text);
        //添加排行榜标题+形状
        var roundRect = new egret.Shape();
        // roundRect.graphics.lineStyle(2, 0xffffff);
        roundRect.graphics.beginFill(0xffffff);
        roundRect.graphics.drawRoundRect(65, 382, 515, 88, 88, 88);
        roundRect.graphics.endFill;
        this.addChild(roundRect);
        var phb_text = createTextFiled("指尖自由泳全国前10排行榜", 91, 382, 37, 0xff4c22, "center", 464, 88);
        this.addChild(phb_text);


        //添加排行榜容器框+内容容器+内容
        var maskDisplay = new egret.DisplayObjectContainer();
        maskDisplay.width = 450;
        // maskDisplay.height = 420;
        var scrollView: egret.ScrollView = new egret.ScrollView();
        //设置滚动内容
        scrollView.setContent(maskDisplay);
        //设置滚动区域宽高
        scrollView.width = 450;
        scrollView.height = 420;
        scrollView.x = 95;
        scrollView.y = 488;
        this.addChild(scrollView);


        //添加按钮(晒战绩)
        var shaizj_buttom = my_button("晒战绩", 37, 0xfe3c63, true, 157, 90, 65, 970, "phb_btn1_png", 4, 0xfff530);
        this.addChild(shaizj_buttom);
        shaizj_buttom.touchEnabled = true;
        //添加按钮(前往领奖台抽奖)
        var cj_buttom = my_button("前往领奖台抽奖", 37, 0xfe3c63, true, 314, 90, 260, 970, "phb_btn2_png", 4, 0xfff530)
        this.addChild(cj_buttom);
        //晒出成绩的弹出窗
        var paihangbang_mask = drawReactShape(null, 0, 0, 640, 1136, 0x000000);
        paihangbang_mask.alpha = 0.8;
        var shaichu = createBitmap("shaichu_png", 240, 39);
        var rongqi = new egret.DisplayObjectContainer;
        this.addChild(rongqi);
        rongqi.addChild(paihangbang_mask);
        rongqi.addChild(shaichu);
        rongqi.anchorOffsetX = 320;
        rongqi.anchorOffsetY = 568;
        rongqi.x = 320;
        rongqi.y = 568;
        rongqi.scaleX = 0;
        rongqi.touchEnabled = true;
        // egret.Tween.get(rongqi).to({scaleX:1},500);
        shaizj_buttom.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            egret.Tween.get(rongqi).to({ scaleX: 1 }, 500);
        }, this);
        rongqi.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            egret.Tween.get(rongqi).to({ scaleX: 0 }, 500);
        }, this)


        var shaichu = createBitmap("shaichu_png", )
        //后台请求获取个人信息
        MYajax({ "type": "userinfo" }, "http://swin.bluej.cn/index.php/Home/app/get_userinfo.html", function (res) {
            name_text.text = res.data.name;
            RES.getResByUrl(res.data.image, function (texture: egret.Texture): void {
                var result = new egret.Bitmap();
                result.texture = texture;
                result.width = 108;
                result.height = 108;
                result.x = 0;
                result.y = 0;
                imgDisplay.addChild(result);
                result.mask = imgBox;
            }, this, RES.ResourceItem.TYPE_IMAGE);
        })
        //后台获取排名和时间
        MYajax({ "type": "me_ranking" }, "http://swin.bluej.cn/index.php/Home/app/me_ranking.html", function (res) {
            console.log(res);
            pm_text.text = res.data;
            time_text.text = res.time;
        })
        //后台请求动态生成排行榜
        MYajax({ "type": "ranking_list" }, "http://swin.bluej.cn/index.php/Home/app/ranking_list.html", function (res) {
            maskDisplay.height = 105 * res.data.length;
            for (var i = 0; i < res.data.length && i < 10; i++) {
                var listbg = i == 0 ? "RLfirst_png" : i == 1 ? "RLsecond_png" : i == 2 ? "RLthird_png" : "RLnumber_png";
                maskDisplay.addChild(list(105 * i, listbg, i + 1, res.data[i].name, res.data[i].game_time, res.data[i].image));

            }
        })
        //点击抽奖按钮事件
        cj_buttom.touchEnabled = true;
        cj_buttom.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            var verificationui = new VerificationUI();
            this.stage.addChild(verificationui);
            this.stage.removeChild(this);
            //将背景成绩放在最后面，将里面的music_logo放到最高层级
            var bgui = new index_bgUI();
            var main = new Main();
            main.addChildAt(bgui, 0);
        }, this)
    }

}
function list(list_y, list_bg, num, name, time, touxiang) {
    var list = new egret.DisplayObjectContainer();
    list.width = 518;
    list.height = 88;
    list.y = list_y;//17
    list.x = 0;
    // maskDisplay.addChild(list1);
    var list1bg = createBitmap(list_bg, 0, 0);
    list.addChild(list1bg);
    var list_num = createTextFiled(num, 0, 0, 31, 0x000000, "center", 85, 88);
    list_num.bold = true;
    list.addChild(list_num);
    var list_imgshape = new egret.Shape();
    list_imgshape.graphics.beginFill(0xffffff);
    list_imgshape.graphics.drawRoundRect(71, 8, 72, 72, 72, 72);
    list_imgshape.graphics.endFill;
    list.addChild(list_imgshape);
    RES.getResByUrl(touxiang, function (texture: egret.Texture): void {
        var result = new egret.Bitmap();
        result.texture = texture;
        result.width = 72;
        result.height = 72;
        result.x = 71;
        result.y = 8;
        list.addChild(result);
        result.mask = list_imgshape;
    }, list, RES.ResourceItem.TYPE_IMAGE);
    var list_name = createTextFiled(name, 158, 0, 32, 0xff4c22, "left", 150, 88);//"Amy"
    list.addChild(list_name);
    var list_time_text = createTextFiled("Time", 380, 11, 14, 0xff4c22);
    list.addChild(list_time_text);
    var list_time = createTextFiled(time, 325, 35, 32, 0xff4c22, "center", 115, 32);//"100:00"
    list.addChild(list_time);
    return list;
}